//
//  LBTabBarViewController.m
//  V-Learn
//
//  Created by Apple on 2018/12/20.
//  Copyright © 2018 yxk. All rights reserved.
//

#import "LBTabBarViewController.h"


@interface LBTabBarViewController ()

@end

@implementation LBTabBarViewController


+ (LBTabBarViewController *)shareInstance{
//    static Singleton *_sharedSingleton = nil;
    static LBTabBarViewController *_tabBar = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 要使用self来调用
        _tabBar = [[self alloc] init];
    });
    return _tabBar;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    // Do any additional setup after loading the view.
    [self CreationVC];
}

- (void)CreationVC{
    NSArray * className = @[@{@"className":@"HomeViewController",@"title":@"首页",@"imageName":@"tabbar_home",@"selectImageName":@"tabbar_home_s"},
                            @{@"className":@"FriendViewController",@"title":@"社圈",@"imageName":@"tabbar_sq",@"selectImageName":@"tabbar_sq_s"},
                            @{@"className":@"StudyViewController",@"title":@"学习",@"imageName":@"tabbar_xx",@"selectImageName":@"tabbar_xx_s"},
                            @{@"className":@"MeViewController",@"title":@"我的",@"imageName":@"tabbar_mine",@"selectImageName":@"tabbar_mine_s"}];
    for (int i = 0; i < className.count; i++) {
        UIViewController * vc = [[NSClassFromString(className[i][@"className"]) alloc] init];
        vc.view.backgroundColor = [UIColor whiteColor];
        [self addOneChlildVc:vc title:className[i][@"title"] imageName:className[i][@"imageName"] selectedImageName:className[i][@"selectImageName"]];
    }
}


/**
 *  添加一个子控制器
 *
 *  @param childVc           子控制器对象
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)addOneChlildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageWithOriginal:imageName];
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageWithOriginal:selectedImageName];
    childVc.tabBarItem.selectedImage = selectedImage;
    childVc.tabBarItem.title = title;
    
    //适配iPhone X 以上机型 tabbar item 图片和 文字间距
    if (IS_IPHONE_X ||IS_IPHONE_Xs||IS_IPHONE_Xs_Max||IS_IPHONE_Xr) {
        childVc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 5);
        
        childVc.tabBarItem.imageInsets=UIEdgeInsetsMake(-1,0,1,0);
    }
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13.0]} forState:UIControlStateNormal];//TabBarItem未选中时的字体大小
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : BRRGBColor(224, 46, 53, 1),NSFontAttributeName : [UIFont systemFontOfSize:13.0]} forState:UIControlStateSelected];//TabBarItem选中时的字体颜色和
    // 添加为tabbar控制器的子控制器
     LBNavigationController *nav = [[LBNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
